﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EquipSecs300
{
    public class UtilitiesConvert
    {
        // 字符串转long
        static public long StringToLong(string data)
        {
            long nNum = 0;
            try
            {
                nNum = long.Parse(data);
            }
            catch (Exception)
            {
            }
            return nNum;
        }

        // 字符串转int
        static public int StringToInt32(string data)
        {
            int nNum = 0;
            if (string.IsNullOrEmpty(data))
            {
                return nNum;
            }
            try
            {
                nNum = int.Parse(data);
            }
            catch (Exception)
            {
            }
            return nNum;
        }

        static public Int16 StringToInt16(string data)
        {
            Int16 nNum = 0;
            if (string.IsNullOrEmpty(data))
            {
                return nNum;
            }
            try
            {
                nNum = Int16.Parse(data);
            }
            catch (Exception)
            {
            }
            return nNum;
        }

        static public UInt16 StringToUInt16(string data)
        {
            UInt16 nNum = 0;
            if (string.IsNullOrEmpty(data))
            {
                return nNum;
            }
            try
            {
                nNum = UInt16.Parse(data);
            }
            catch (Exception)
            {
            }
            return nNum;
        }

        static public UInt32 StringToUInt32(string data)
        {
            UInt32 nNum = 0;
            if (string.IsNullOrEmpty(data))
            {
                return nNum;
            }
            try
            {
                nNum = UInt32.Parse(data);
            }
            catch (Exception)
            {
            }
            return nNum;
        }

        // 字符串转Double
        static public double StringToDouble(string data)
        {
            double nNum = 0;
            if (string.IsNullOrEmpty(data))
            {
                return nNum;
            }
            try
            {
                nNum = double.Parse(data);
            }
            catch (Exception)
            {
            }
            return nNum;
        }

        /// <summary>
        /// 针对0,1转
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool StrToBool(string str)
        {
            int res;
            int.TryParse(str, out res);
            if (res == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 针对0,1转
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string BoolToStr(bool bol)
        {
            if (bol)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }


        /// <summary>
        /// 针对true,false转
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool StrStrToBoolean(string str)
        {
            bool res;
            bool.TryParse(str, out res);
            return res;
        }
        /// <summary>
        /// 针对true,false转
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string BoolToStrStr(bool bol)
        {
            if (bol)
            {
                return "true";
            }
            else
            {
                return "false";
            }
        }

        /// <summary>
        /// 将List int有料转化为List bool类型(无料层转false)
        /// </summary>
        /// <param name="listInt"></param>
        /// <returns></returns>
        public static List<bool> ConvertIntListToBoolList(List<int> listInt)
        {
            int temp = 1;

            if (listInt == null)
            {
                return null;
            }
            List<bool> boolList = new List<bool>();
            listInt.Sort();
            for (int i = 0; i < 25; i++)
            {
                for (int j = temp; j < 25 + 1; j++)
                {
                    if (i < listInt.Count && listInt[i] == j)
                    {
                        boolList.Add(true);
                        temp = listInt[i];
                        temp++;
                        break;
                    }
                    else
                    {
                        boolList.Add(false);
                        if (boolList.Count == 25)
                        {
                            break;
                        }
                    }
                }
                if (boolList.Count == 25)
                {
                    break;
                }
            }
            return boolList;
        }
        /// <summary>
        /// 将List bool有料转化为List int类型(无料层不转化)
        /// </summary>
        /// <param name="listbool"></param>
        /// <returns></returns>
        public static List<int> ConvertBoolListToIntList(List<bool> listbool)
        {
            if (listbool == null)
            {
                return null;
            }
            List<int> intList = new List<int>();
            for (int i = 0; i < listbool.Count; i++)
            {
                if (listbool[i])
                {
                    intList.Add(i + 1);
                }
            }
            return intList;
        }

        /// <summary>
        /// 将一串16进制数字转化成对应一串Ascaii字符串
        /// </summary>
        /// <param name="strHex"></param>
        /// <returns></returns>
        public static string ConvertToAscii(string strHex)
        {
            if (string.IsNullOrEmpty(strHex))
            {
                return null;
            }
            string hex = strHex;
            List<byte> bytelists = new List<byte>();
            string s = null;
            string strRet = null;

            for (int i = 0; i < hex.Length; i++)
            {
                if (hex[i] != ' ')
                {
                    s += hex[i];
                }
            }
            hex = s;

            for (int i = 0; i < hex.Length / 2; i++)
            {
                string str = hex.Substring(i * 2, 2);
                //sb.Append(str+"/");
                byte b = Convert.ToByte(str, 16);
                bytelists.Add(b);
            }

            List<char> listchar = new List<char>();
            foreach (byte item in bytelists)
            {
                listchar.Add(Convert.ToChar(item));
            }

            for (int i = 0; i < listchar.Count; i++)
            {
                strRet += listchar[i];
            }
            return strRet;
        }

    }
}
